package com.esm.sm800.database.manager;

import com.esm.sm800.activity.AppContext;
import com.esm.sm800.database.dbmodel.Member;
import com.esm.sm800.database.dbmodel.Needle;
import com.esm.sm800.database.greendao.MemberDao;
import com.esm.sm800.database.greendao.NeedleDao;
import com.esm.sm800.util.Utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class MemberCenter {

    private static MemberCenter instance = new MemberCenter();

    public static MemberCenter getInstance() {
        return instance;
    }

    public Member getMemberbyid(long id) {
        Member member = null;
        try {
            MemberDao MemberDao = AppContext.getInstances().getDaoSession().getMemberDao();
            ArrayList<Member> list = new ArrayList<Member>();
            list = (ArrayList) MemberDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.MemberDao.Properties.Id.eq(id))
                    .build().list();
            if (list.size() > 0) {
                member = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return member;
    }

    public Member getMemberbynfc(String nfc) {
        if (nfc == null || nfc.length() < 2) {
             return null;
        }
        Member member = null;
        try {
            MemberDao MemberDao = AppContext.getInstances().getDaoSession().getMemberDao();
            ArrayList<Member> list = new ArrayList<Member>();
            list = (ArrayList) MemberDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.MemberDao.Properties.Nfc.eq(nfc))
                    .build().list();
            if (list.size() > 0) {
                member = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return member;
    }

     public Member getMember(String account) {
        if (account == null || account.length() < 1) {
            return null;
        }
        Member member = null;
        try {
            MemberDao MemberDao = AppContext.getInstances().getDaoSession().getMemberDao();
            ArrayList<Member> list = new ArrayList<Member>();
            list = (ArrayList) MemberDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.MemberDao.Properties.Account.eq(account))
                    .build().list();
            if (list.size() > 0) {
                member = list.get(0);
            } else if (account.equals("admin")) {
                //如果没有则插入一个数据
                member = new Member();
                member.setAccount("admin");
                member.setName("admin");
                member.setPassword("123");
                //获取当前日期
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date curDate = new Date();
                String strDate = formatter.format(curDate);
                //
                member.setCreat_time(strDate);
                member.setUpdate_time(strDate);
                Long id = saveMember(member);
                member.setId(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return member;
    }

    public Long saveMember(Member member) {
        if (member == null) {
            return 0L;
        }

        try {
            MemberDao MemberDao = AppContext.getInstances().getDaoSession().getMemberDao();
            long id = MemberDao.insertOrReplace(member);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    public boolean delMember(Member member) {
        if (member == null) {
            return false;
        }

        try {
            MemberDao MemberDao = AppContext.getInstances().getDaoSession().getMemberDao();
            MemberDao.delete(member);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

    public boolean clear() {
            try {
            MemberDao MemberDao = AppContext.getInstances().getDaoSession().getMemberDao();
            MemberDao.deleteAll();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

    public ArrayList<Member> memberlist = null;

    public ArrayList<Member> getMemberlist(String account) {
        ArrayList<Member> list = new ArrayList<Member>();
        try {
            MemberDao MemberDao = AppContext.getInstances().getDaoSession().getMemberDao();
            if (account == null || account.length() < 1) {
                list = (ArrayList) MemberDao.queryBuilder().build().list();
            } else {
                list = (ArrayList) MemberDao.queryBuilder()
                        .where(com.esm.sm800.database.greendao.MemberDao.Properties.Account.like(account))
                        .build().list();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }
}
